/**
 * 用户列表获取接口测试用例
 *
 * Created by snowingsea on 2020/02/02.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const { statusKind, roleKind } = require('./../../../../phoenix/models/users');
// const license = require('./../../../../phoenix/models/license');

module.exports = describe('/GET users.json', function () {
  const url = '/users.json';
  const administrator = new Administrator();
  const newUserIds = [];
  before(function (done) {
    administrator.createRandomUsers(35)
      .then((users) => {
        users.forEach(({ userId }) => {
          newUserIds.push(userId);
        });
        return administrator.randomUpdateStatus4Users(users);
      })
      .then(() => {
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  // common.should.be.administrator(uri, url, 'get');
  const sortByKeys = ['userId', 'nickname', 'status', 'role', 'lang'];
  common.should.complete.filter(uri, url, 'get', administrator, sortByKeys);

  // 正常获取用户列表
  it('it should GET users', function (done) {
    let total;
    administrator.getAllUsers()
      .then((users) => {
        total = users.length;
        return administrator.login();
      })
      .then(() => {
        chai.request(uri)
          .get(url)
          .set('Authorization', `Bearer ${administrator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.usersMessage(res.body);
            res.body.pagination.total.should.be.eql(total);
            done();
          });
      })
      .catch(done);
  });

  // 搜索
  it('it should search the user by keyword', function (done) {
    let newUser;
    administrator.createRandomUser()
      .then((user) => {
        newUser = user;
        newUserIds.push(user.userId);
        return administrator.login();
      })
      .then(() => {
        chai.request(uri)
          .get(url)
          .query({ q: newUser.username })
          .set('Authorization', `Bearer ${administrator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.usersMessage(res.body);
            res.body.pagination.total.should.be.eql(1);
            res.body.users[0].username.should.be.eql(newUser.username);
            done();
          });
      })
      .catch(done);
  });

  // 许可证过期
  /*
  it('it should succeed get users when the license expires', function (done) {
    administrator.getAllUsers()
      .then(() => administrator.replaceLicense(license.statusKind.exceedTime))
      .then(() => administrator.login())
      .then(() => {
        chai.request(uri)
          .get(url)
          .set('Authorization', `Bearer ${administrator.token.accessToken}`)
          .end((err, res) => {
            administrator.replaceLicense(license.statusKind.normal)
              .then(() => {
                res.should.have.status(200);
                common.should.be.usersMessage(res.body);
                done();
              })
              .catch(done);
          });
      })
      .catch(done);
  });
   */

  // 筛选
  const filters = [];
  let allRole = 0;
  Object.keys(roleKind).forEach((key) => {
    allRole |= roleKind[key]; // eslint-disable-line no-bitwise
  });
  for (let role = 0; role <= allRole; role += 1) {
    filters.push({ roles: [role] });
  }
  filters.push({ roles: [roleKind.tester, roleKind.administrator] });
  Object.keys(statusKind).forEach((key) => {
    filters.push({ status: statusKind[key] });
  });
  filters.forEach((filter) => {
    it(`it should get users with ${JSON.stringify(filter)}`, function (done) {
      administrator.login()
        .then(() => {
          chai.request(uri)
            .get(url)
            .query(filter)
            .set('Authorization', `Bearer ${administrator.token.accessToken}`)
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.usersMessage(res.body);
              res.body.users.forEach((user) => {
                Object.keys(filter).forEach((key) => {
                  if (key === 'roles') {
                    let role = 0;
                    filter.roles.forEach((itemRole) => {
                      role |= itemRole; // eslint-disable-line no-bitwise
                    });
                    (user.role & role).should.above(0); // eslint-disable-line no-bitwise
                  } else {
                    if (key === 'status' && filter[key] === statusKind.offline && user[key] === statusKind.unactivated) {
                      user[key] = statusKind.offline;
                    }
                    user[key].should.be.eql(filter[key]);
                  }
                });
              });
              done();
            });
        })
        .catch(done);
    });
  });
});
